package cn.ze.yygh.user.api;

import cn.ze.yygh.common.result.Result;
import cn.ze.yygh.common.utils.AuthContextHolder;
import cn.ze.yygh.model.user.Patient;
import cn.ze.yygh.user.service.PatientService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    //获取就诊人列表
    @ApiOperation(value = "获取就诊人列表")
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request){
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findByUserId(userId);
        return Result.ok(list);
    }


    //添加就诊人

    @ApiOperation(value = "添加就诊人")
    @PostMapping("auth/save")
    public Result save(@RequestBody Patient patient,
                       HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }
    //根据id获取就诊人的信息
    @ApiOperation(value = "根据id获取就诊人的信息")
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable("id")Long id){
        //Patient patient = patientService.getById(id);
        //上方函数 直接取的话 数据不完整 需要进一步封装..
        Patient patient = patientService.getPatientId(id);
        return Result.ok(patient);
    }

    //修改就诊人
    @ApiOperation(value = "修改就诊人")
    @PostMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }


    //删除就诊人
    @ApiOperation(value = "删除就诊人")
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable("id")Long id){
        patientService.removeById(id);
        return Result.ok();
    }


    //根据就诊人id获取就诊人信息 (order 远程调用)
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id")Long id){
        Patient patient = patientService.getPatientId(id);
        return patient;
    }
}
